Getting TEKnical by unknow
Author:unknow
Language: eng
Format: epub
Publisher: php[architect]
Published: 2023-04-15T00:00:00+00:00
11. Logs
âTreat logs as event streamsâ
From the perspective of a 12 Factor Application, all logs should be directed to stdout (although, as a personal hot take, applications should be directing their output to the most appropriate standard stream). stdout is one of the standard streams that POSIX environments define as writeable file descriptors. These streams provide input and output paths for a program and its operating environment.
The reason for logging into a standard stream instead of something like a log file is to allow flexibility in the running environment. For development, having your PHP application log to stdout or stderr makes it much easier to launch the dev server and just watch the logs as the application runs. All the logging goes to the terminal that the developer is watching.
In a production environment, these streams can easily be redirected by the environment. For many hosting solutions, the stdout/stderr output is pushed up to the web server, and the web server decides what to do. In other cases, PHP is directed to log all of that output to its own separate file.
The key is that the application itself is as accommodating as possible with logging and does not define the ultimate location or process for loggingâit simply outputs to a known, accepted interface that works with almost anything.
For PHP applications, this can be accomplished in a few ways. The quickest and easiest way is to start adding calls to error_log() in the appropriate places in your application. This function, by default, writes to whatever PHP has been configured to be as the error log. This can be stdout, stderr, a file, or whatever. The function lets the PHP engine handle it all.
This is appropriate yet somewhat heavy-handed in that either you, as the developer, need to manually add in checks to see if something should be logged (like has the user configured debug-level logging, or just warnings), or you end up logging everything. While disk space is a cheap commodity today, a developerâs time is not. Reading through millions of lines of logs can be exhausting, even with automated tools.
A better option is implementing a PSR-3 compatible logger. 99% of the time, this means implementing monolog/monolog, PHPâs most widely used logging system. Monolog allows a developer to log messages at different pre-defined levels, like debug, info, error, or emergency. Then, the user can decide what levels they would like to see logged.
While Monolog can take a variety of different logging methods, if we take 12 Factor Application design into account, we would want to log to php://stdout. If you want to take proper Unix design patterns, you would want to log to php://stderr instead. Either case will work.
Why do I keep saying to use stderr instead of stdout for logging? The way the standard streams are designed, stdout is meant for application output, meaning the result of the program. If you are writing a script that calculates the number of words in a document, you would output the result, say 3000, to stdout.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Analysis & Strategy | Bonds |
Commodities | Derivatives |
Futures | Introduction |
Mutual Funds | Online Trading |
Options | Portfolio Management |
Real Estate | Stocks |
Rich Dad Poor Dad by Robert T. Kiyosaki(6110)
Pioneering Portfolio Management by David F. Swensen(6036)
How To Win Friends and Influence People by Dale Carnegie(4289)
The Money Culture by Michael Lewis(3792)
The Dhandho Investor by Mohnish Pabrai(3534)
The Wisdom of Finance by Mihir Desai(3495)
Liar's Poker by Michael Lewis(3192)
The Intelligent Investor by Benjamin Graham Jason Zweig(2908)
The ONE Thing by Gary Keller(2893)
Mastering Bitcoin: Programming the Open Blockchain by Andreas M. Antonopoulos(2868)
Fooled by Randomness: The Hidden Role of Chance in Life and in the Markets by Nassim Nicholas Taleb(2835)
Rich Dad Poor Dad: What The Rich Teach Their Kids About Money - That The Poor And Middle Class Do Not! by Robert T. Kiyosaki(2804)
How to Win Friends and Influence People by Dale Carnegie(2775)
Investing For Dummies by Eric Tyson(2770)
How to Day Trade for a Living: Tools, Tactics, Money Management, Discipline and Trading Psychology by Andrew Aziz(2764)
Market Wizards by Jack D. Schwager(2519)
Zero Hour by Harry S. Dent Jr. & Andrew Pancholi(2513)
How to Pay Zero Taxes, 2018 by Jeff A. Schnepper(2456)
Rich Dad's Guide to Investing by Robert T. Kiyosaki(2398)
